package com.zy.web.controller.bs;

import java.util.List;

import com.zy.entity.bs.Cart;
import com.zy.entity.crm.Customer;
import com.zy.pojo.vos.common.ResultVo;
import com.zy.service.bs.CartService;
import com.zy.utils.ConvertUtil;
import com.zy.web.controller.base.BaseController;
import com.zy.web.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@RequestMapping({ "eb/cart", "m/eb/cart" })
@Controller
public class CartController extends BaseController {

	@Autowired
	private CartService cartService;

	/**
	 * 获取购物车数据
	 * 
	 * @return
	 */
	@RequestMapping("listCart")
	@ResponseBody
	public List<Cart> listCart() {
		return this.cartService.listCart(super.getCurrentCustomer().getId());
	}

	/**
	 * 新增
	 * 
	 * @param cart
	 * @return
	 */
	@RequestMapping(value = "saveCart", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo saveCart(Cart cart) {
		try {
			cart.setCustomerId(super.getCurrentCustomer().getId());
			this.cartService.saveCart(cart);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 更新
	 * 
	 * @param cart
	 * @return
	 */
	@RequestMapping(value = "updateCart", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo updateCart(Cart cart) {
		try {
			this.cartService.updateCart(cart);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "removeCart", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo removeCart(long id) {
		try {
			this.cartService.removeCart(id);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 恢复
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "recover", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo recover(long id) {
		try {
			this.cartService.recover(id);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 计算
	 * 
	 * @param goodsIds
	 * @return
	 */
	@RequestMapping(value = "calc")
	@ResponseBody
	public ResultVo calc(String goodsIds) {
		try {
			return ResultUtil.success(this.cartService.calc(ConvertUtil.convertList(goodsIds.split(",")),
					super.getCurrentCustomer().getId()));
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	@RequestMapping(value = "getCount")
	@ResponseBody
	public ResultVo getCount() {
		try {
			Customer customer = super.getCurrentCustomer();
			if (customer == null) {
				return ResultUtil.success(0);
			}
			Cart search = new Cart();
			search.setCustomerId(customer.getId());
			return ResultUtil.success(this.cartService.getCount(search));
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

}